package com.allianz.webapps.controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.allianz.webapps.https.interfaces.appgetRiders.AppGetRiders;
import com.allianz.webapps.https.interfaces.util.HttpsUtil;
import com.allianz.webapps.orderspojo.AppBase;
import com.allianz.webapps.orderspojo.Cover;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@Controller
@RequestMapping("main")
public class AppGetRidersController {
	@Autowired
	private ObjectMapper objectMapper;
	@Autowired
	private AppGetRiders appRiders;
    @RequestMapping("appgetriders")
    @ResponseBody
	public List<Cover> appGetRiders(@RequestParam(value="jsonParam") String jsonParam,HttpServletRequest request,HttpServletResponse response) throws JsonProcessingException, Exception{
		Map<String, String> bodymap=new HashMap<String, String>();
		AppBase appBase = objectMapper.readValue(jsonParam, AppBase.class);
		String body1=HttpsUtil.httpsPost(appRiders.requestAppGetRiders(appBase));
		JSONObject json=new JSONObject(body1);
		String body="";
		if (json.get("Body").equals(null)) {
			if(!"S".equals(json.getString("Status")))
				throw new Exception(json.getString("ErrMsg"));
			else
				return null;
		}else{
			body=json.getString("Body");
			List<Cover> covers = objectMapper.readValue(body, List.class);
			return covers;
		}
	}
}
